//
//  MovieCollectionController.m
//  豆瓣电影
//
//  Created by dllo on 16/6/24.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MovieCollectionController.h"
#import "MovieModel.h"
#import "CustomCollectionViewCell.h"

@interface MovieCollectionController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, retain) NSMutableArray *movieArray;

@property (nonatomic, retain) UICollectionView *collectionView;


@end

@implementation MovieCollectionController

-(void)dealloc
{
    [_movieArray release];
    [_collectionView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self config];
    
    [self handleData];

    [self createCollectionView];
    
    [self createNavigationItem];
}

/**视图将要出现*/
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.41 green:0.61 blue:0.68 alpha:1.0];
}

-(void)config
{
    self.movieArray = [NSMutableArray array];
    
    self.navigationItem.title = @"电影";
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.hidesBackButton = YES;
}

#pragma mark - 解析数据
-(void)handleData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movielist" ofType:@"txt"];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSError *error = nil;
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
    NSArray *array = [dic objectForKey:@"result"];
    
    for (NSDictionary *dic in array) {
        MovieModel *movie = [[MovieModel alloc]init];
        
        [movie setValuesForKeysWithDictionary:dic];
        
        [self.movieArray addObject:movie];
    }
    //    NSLog(@"%@", self.movieArray);
}

/**导航栏右按钮*/
-(void)createNavigationItem
{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_list@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
}

-(void)backAction:(UINavigationItem *)item
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}


#pragma mark - collectionView
-(void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    /**设置item的大小*/
    flowLayout.itemSize = CGSizeMake(100, 150);
    
    /**滚动方向*/
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    /**设置item和四周边缘的距离*/
    flowLayout.sectionInset = UIEdgeInsetsMake(70, 10, 50, 10);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.collectionView];
    
    self.collectionView.dataSource = self;
    
    self.collectionView.delegate = self;
    
    [self.collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];

}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    
    cell.contentView.backgroundColor = [UIColor whiteColor];
    
    MovieModel *movieModel = [self.movieArray objectAtIndex:indexPath.row];
    
    cell.cellModel = movieModel;
    
    return cell;
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.movieArray.count;
}

@end
